/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Builder;

import DAO.InformationFilter.InterestingDAO;
import DAO.InformationFilter.PageDAO;
import DAO.InformationFilter.PrdDAO;
import Info.PageInfo;
import Info.PeriodInfo;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class Scorer
{
    private class PeriodComparator implements Comparator<PeriodInfo>
    {
        public int compare(PeriodInfo o1, PeriodInfo o2) {
            return o1.GetBegin().compareTo(o2.GetBegin());
        }
    }

    public Double GetScorer(int idUser, int idPage)
    {
        PageDAO pageDAO = new PageDAO();
        PageInfo pageInfo = pageDAO.GetByID(idPage);

        if (pageInfo == null)
            return null;

        Timestamp publishDate = pageInfo.GetTime();

        PrdDAO periodDAO = PrdDAO.GetInstance();
        List<PeriodInfo> allPeriod = periodDAO.GetAllPeriod();
        Collections.sort(allPeriod, new PeriodComparator());

        InterestingDAO interestingDAO = new InterestingDAO();


        for (PeriodInfo periodInfo : allPeriod)
        {
            if (publishDate.after(periodInfo.GetBegin()))
            {
                Integer category = pageDAO.GetCategory(idPage);
                if (category == null) continue;
                Double percent = interestingDAO.GetScore(idUser, category, periodInfo.GetID());
                return percent;
            }
        }

        return null;
    }

}
